package com.shencai.demo.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shencai.demo.base.entity.SysMenu;
import com.shencai.demo.base.entity.SysRoleMenu;
import com.shencai.demo.base.service.SysRoleMenuService;
import com.shencai.demo.base.mapper.SysRoleMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author sckj
* @description 针对表【sys_role_menu(角色菜单关联表)】的数据库操作Service实现
* @createDate 2022-10-19 11:04:48
*/
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu>
    implements SysRoleMenuService{

    @Autowired
    private SysRoleMenuMapper roleMenuMapper;
    /**
     * 删除角色所具有的所有权限，再添加权限实现修改用户权限
     * @param roleId
     * @param menuIds
     * @return
     */
    @Override
    public boolean updateMenuByRoleId(Integer roleId, List<Integer> menuIds) {
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleMenu::getRoleId,roleId);
        baseMapper.delete(queryWrapper);
        ArrayList<SysRoleMenu> sysRoleMenus = new ArrayList<>();
        for (Integer menuId : menuIds) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenus.add(sysRoleMenu);
        }
        boolean update = this.saveBatch(sysRoleMenus);
        return update;
    }

    /**
     * 根据角色ID查询对应权限
     * @param id
     * @return
     */
    @Override
    public List<SysMenu> listMenuByRoleId(Integer id) {
        return roleMenuMapper.listMenuByRoleId(id);
    }
}




